package com.feng.leetcode;

/**
 * @author feng
 * @decrition
 * @date 2022/5/922:09
 */
public class L43 {
    public static void main(String[] args) {
        String num1 = "123456789";
        String num2 = "987654321";
        /*String num1 = "123";
        String num2 = "123";*/
        String multiply = multiply(num1, num2);
        System.out.println(multiply);
    }

    public static String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        int len1 = num1.length();
        int len2 = num2.length();
        String sum = "";
        for (int i = len1 - 1; i >= 0; i--) {
            int n1 = num1.charAt(i) - '0';
            for (int j = len2-1; j >= 0; j--) {
                int n2 = num2.charAt(j) - '0';
                int cj = n1 * n2; // 两数的乘机
                int yi = len1 - i - 1;
                int er = len2 - j - 1;
                int index = yi + er;
                sum = sumString(sum, cj + "", index);
            }
            System.out.println(sum);
        }
        return sum;
    }

    private static String sumString(String sum, String num, int index) {
        int len = sum.length();
        int jia = num.length() + index;
        for (int i = 0; i < index; i++) {
            num += 0;
        }
        return sumString(sum, num);
    }

    private static int getShicifang(int len) {
        int shicifang = 1;
        for (int k = 0; k < len; k++) {
            shicifang*=10;
        }
        return shicifang;
    }

    private static String sumString(String sum, String num) {
        StringBuilder stringBuilder = new StringBuilder();
        int temp = 0;
        int i = sum.length() - 1;
        int j = num.length() - 1;
        while (i >= 0 || j >= 0 || temp != 0) {
            int s = 0;
            int n = 0;
            if (i >= 0) {
                s = sum.charAt(i) - '0';
            }
            if (j >= 0) {
                n = num.charAt(j) - '0';
            }
            int sn = s + n + temp;
            int mod = sn % 10;
            stringBuilder.append(mod);
            temp = 0;
            if (sn > 9) {
                temp = sn/10;
            }
            i--;
            j--;
        }
        return stringBuilder.reverse().toString();
    }
}
